package org.mypro.front;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.mypro.dao.OrderformMapper;
import org.mypro.dao.UserMapper;
import org.mypro.entity.Orderform;
import org.mypro.entity.User;
import org.mypro.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

/**

	


**/

@Controller
@RequestMapping(value = "orderform")
public class OrderformController {
	private static final long serialVersionUID = -9167213418951271794L;

	private int pageSize = 10;
	private int currentPage = 1;

	@Autowired
	private OrderformMapper entityDao;

	@Autowired
	private UserMapper userDao;

	@RequestMapping(value = "list")
	public ModelAndView list(HttpServletRequest request, Orderform entity) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("houtai/orderform/list");
		try {
			User user = (User) request.getSession().getAttribute("LOGIN_USER");
			if ("2".equals(user.getType())) {
				entity.setConsignorid(user.getId());
			}
			if ("3".equals(user.getType())) {
				entity.setExpressid(user.getId());
				entity.setStatus("正在投递");
			}
			if ("4".equals(user.getType())) {
				entity.setConsigneeid(user.getId());
				entity.setStatus("快件签收");
				
			}
			int listCount = entityDao.countList(entity);
			PageUtil pageUtil = new PageUtil();
			if (listCount > 0) {
				pageUtil.setPageSize(pageSize);// 每页显示的记录数量
				pageUtil.setTotalRecord(listCount);// 总记录数
				pageUtil.setCurrentPage(currentPage);// 当前页码
				entity.setBeginRow(pageUtil.getStart());
				entity.setPageSize(pageUtil.getPageSize());
				List<Orderform> entityList = (List<Orderform>) entityDao
						.findList(entity);
				mv.addObject("list", entityList);
			} else {
				pageUtil.setPageSize(pageSize);// 每页显示的记录数量
				pageUtil.setTotalRecord(0);// 总记录数
				pageUtil.setCurrentPage(currentPage);// 当前页码
			}
			mv.addObject("message", "查询成功！");
			mv.addObject("status", true);
			mv.addObject("pageUtil", pageUtil);
		} catch (Exception e) {
			e.printStackTrace();
			mv.addObject("message", "查询失败！");
			mv.addObject("status", "查询失败！");
			mv.addObject("status", false);
			mv.addObject("status", false);
		}
		return mv;
	}

	@RequestMapping(value = "query")
	public ModelAndView query(HttpServletRequest request, Orderform entity) {

		ModelAndView mv = list(request, entity);
		mv.setViewName("houtai/orderform/query");
		return mv;
	}

	@RequestMapping(value = "listAll")
	public ModelAndView listAll(Orderform entity) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("houtai/orderform/listAll");
		try {
			List<Orderform> entityList = (List<Orderform>) entityDao
					.findAllList(entity);
			mv.addObject("list", entityList);
			mv.addObject("message", "查询成功！");
			mv.addObject("status", true);
		} catch (Exception e) {
			e.printStackTrace();
			mv.addObject("message", "查询失败！");
			mv.addObject("status", false);
		}
		return mv;
	}

	@RequestMapping(value = "edit")
	public ModelAndView edit(Orderform entity) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("houtai/orderform/edit");
		try {
			mv
					.addObject("entity", entityDao.selectByPrimaryKey(entity
							.getId()));

			User pjr = new User();
			pjr.setType("2");
			mv.addObject("pjrs", userDao.findAllList(pjr));

			User kdy = new User();
			kdy.setType("3");
			mv.addObject("kdys", userDao.findAllList(kdy));

			User sjr = new User();
			sjr.setType("4");
			mv.addObject("sjrs", userDao.findAllList(sjr));

			mv.addObject("message", "查询成功！");
			mv.addObject("status", true);
		} catch (Exception e) {
			e.printStackTrace();
			mv.addObject("message", "查询失败！");
			mv.addObject("status", false);
		}
		return mv;
	}

	@RequestMapping(value = "add")
	public ModelAndView add() {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("houtai/orderform/edit");

		User pjr = new User();
		pjr.setType("2");
		mv.addObject("pjrs", userDao.findAllList(pjr));

		User kdy = new User();
		kdy.setType("3");
		mv.addObject("kdys", userDao.findAllList(kdy));

		User sjr = new User();
		sjr.setType("4");
		mv.addObject("sjrs", userDao.findAllList(sjr));

		return mv;
	}

	@RequestMapping(value = "saveUpdate", method = RequestMethod.POST)
	public ModelAndView saveUpdate(Orderform entity) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("redirect:/orderform/list.action");
		try {

			entity.setExt1(userDao.selectByPrimaryKey(entity.getConsignorid())
					.getUsername());
			entity.setExt2(userDao.selectByPrimaryKey(entity.getConsigneeid())
					.getUsername());
			entity.setExt3(userDao.selectByPrimaryKey(entity.getExpressid())
					.getUsername());

			entity.setCreatetime(new Date());
			if (entity.getId() != null) {
				entityDao.updateByPrimaryKey(entity);
			} else {
				entityDao.insert(entity);
			}
			mv.addObject("message", "修改成功！");
			mv.addObject("status_", true);
		} catch (Exception e) {
			e.printStackTrace();
			mv.addObject("message", "修改失败！");
			mv.addObject("status_", false);
		}
		return mv;
	}

	@RequestMapping(value = "delete")
	public ModelAndView delete(Orderform entity) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("redirect:/orderform/list.action");
		try {
			if (entityDao.deleteByPrimaryKey(entity.getId()) > 0) {
				mv.addObject("message", "删除成功！");
				mv.addObject("status", true);
			} else {
				mv.addObject("message", "删除失败！");
				mv.addObject("status", false);
			}
		} catch (Exception e) {
			e.printStackTrace();
			mv.addObject("message", "删除失败！");
			mv.addObject("status", false);
		}
		return mv;
	}

}
